<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" clearable placeholder="参数名" />
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-has-permission="['coupon:homeadv:save']" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          v-has-permission="['coupon:homeadv:delete']"
          :disabled="dataListSelections.length <= 0"
          type="danger"
          @click="deleteHandle()"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column
        align="center"
        header-align="center"
        type="selection"
        width="50"
      />
      <el-table-column
        align="center"
        header-align="center"
        label="id"
        prop="id"
      />
      <el-table-column
        align="center"
        header-align="center"
        label="名字"
        prop="name"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="200"
        label="缩略图"
      >
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" style="width: 60px; height: 60px">
            <div slot="error" class="image-slot" @click="handleClipboard(scope.row.pic, $event)">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        header-align="center"-->
      <!--        label="开始时间"-->
      <!--        prop="startTime"-->
      <!--      />-->
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        header-align="center"-->
      <!--        label="结束时间"-->
      <!--        prop="endTime"-->
      <!--      />-->
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        header-align="center"-->
      <!--        label="点击数"-->
      <!--        prop="clickCount"-->
      <!--      />-->
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        header-align="center"-->
      <!--        label="广告详情连接地址"-->
      <!--        prop="url"-->
      <!--      />-->
      <el-table-column
        align="center"
        header-align="center"
        label="排序"
        prop="sort"
      />
      <el-table-column
        align="center"
        header-align="center"
        label="发布者"
        prop="publisherId"
      />
      <!--      <el-table-column-->
      <!--        align="center"-->
      <!--        header-align="center"-->
      <!--        label="审核者"-->
      <!--        prop="authId"-->
      <!--      />-->
      <el-table-column label="状态" align="center" width="160">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            loading
            active-text="开启"
            inactive-text="关闭"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          /></template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150"
      >
        <template slot-scope="scope">
          <el-button size="small" type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button size="small" type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
  </div>
</template>

<script>
import clipboard from '@/utils/clipboard'
import AddOrUpdate from './home-adv-add-or-update'
import { delHome_adv, listHome_adv, updateAdvStatus } from '@/api/sms/home_adv'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  async created() {
    this.getDataList()
  },
  activated() {},
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      listHome_adv({
        'pageNum': this.pageIndex,
        'pageSize': this.pageSize,
        'key': this.dataForm.key
      }).then(({ data }) => {
        this.dataList = data.list
        this.totalPage = data.totalCount
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    handleStatusChange(val) {
      updateAdvStatus({ id: val.id, status: val.status })
    },
    // 删除
    deleteHandle(id) {
      const ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delHome_adv(ids).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    },
    handleClipboard(text, event) {
      clipboard(text, event)
    }
  }
}
</script>
